
import Class.Conexion;
import Class.Encoding;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Random;
import javax.swing.JOptionPane;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;

/*
 * Principal.java
 *
 * Created on 5 de abril de 2008, 16:44
 */
import javax.swing.JDialog;
import javax.swing.JFrame;



/**
 *
 * @author  Magaña
 */
public class Principal extends javax.swing.JFrame {
    DefaultListModel listModel=new DefaultListModel();
       Control miControl;
    /** Creates new form Principal */
    public Principal() {
        miControl = new Control(this);
        initComponents();
        loadObject();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        txtUser = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        txtPass = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jButton3 = new javax.swing.JButton();
        btnEntrar = new javax.swing.JButton();
        jProgressBar1 = new javax.swing.JProgressBar();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setLayout(new java.awt.GridLayout(2, 2));

        jLabel2.setText("Nombre");
        jPanel1.add(jLabel2);
        jPanel1.add(txtUser);

        jLabel1.setText("Password");
        jPanel1.add(jLabel1);
        jPanel1.add(txtPass);

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        jPanel2.setLayout(new java.awt.BorderLayout());

        jButton3.setText("Clientes");
        jButton3.addActionListener(miControl);
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jPanel2.add(jButton3, java.awt.BorderLayout.CENTER);

        btnEntrar.setText("Entrar");
        btnEntrar.addActionListener(miControl);
        btnEntrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEntrarActionPerformed(evt);
            }
        });
        jPanel2.add(btnEntrar, java.awt.BorderLayout.PAGE_START);
        jPanel2.add(jProgressBar1, java.awt.BorderLayout.PAGE_END);

        getContentPane().add(jPanel2, java.awt.BorderLayout.SOUTH);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        VistaCliente f=new VistaCliente();
        f.show();
        this.hide();
    }//GEN-LAST:event_jButton3ActionPerformed

    private void btnEntrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEntrarActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_btnEntrarActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public void login(){
        Encoding enc = new Encoding();
        String user = txtUser.getText();
        String pass = txtPass.getText();
        if(user.length() > 0 && pass.length()>0){
            if(enc.verificaC(user) && enc.verificaC(pass)){
                Conexion con = new Conexion("jdbc:odbc:DSNblockbuster","sa","sadb");
                try{
                    PreparedStatement ps = con.getConnection().prepareStatement("SELECT Activo, EnSesion, Nivel, CambiarPass FROM Usuarios WHERE Nomina = ? and Password = ?");
                    ps.setInt(1, Integer.parseInt(user));
                    ps.setString(2, enc.encode(pass));
                    ResultSet rs = ps.executeQuery();
                    try{
                        if(rs.next()){
                            if(!rs.getBoolean("Activo"))
                                System.out.println("Usuario inactivo");
                            else
                                if(rs.getBoolean("EnSesion"))
                                    System.out.println("Usuario en sesion.");
                                else{
                                    if(rs.getBoolean("CambiarPass"))
                                        System.out.println("Favor de cambiar el password.");
                                    //System.out.println("Usuario Conectado .... ");
                                    //Mostrar el otro form .. ke no se kual sea jajaja
                                }
                        }else{
                            System.out.println("no hay registros");
                        }
                    }catch(Exception e){
                        System.out.println(e.getMessage());
                    }
                    finally{
                        ps.close();
                        ps = null;
                    }
                }catch(SQLException er){
                    //Error: er,getMessage
                    System.out.println(er.getMessage());
                }finally{
                    con.cerrar();
                    con = null;
                }
            }else{
                //Error: usuario y/o password incorrecto.
                System.out.println("Error: usuario y/o password incorrecto.");
            }
        }else{
            System.out.println("Favor de llenar los datos requeridos.");
        }
    }
    
    Random a=new Random();
    int i=0;
    public void otroMetodo()
    {
        String r= txtUser.getText();
         FileOutputStream fo = null;
        {
            ObjectOutputStream oo = null;
            try {
                fo = new FileOutputStream("c:\\lista.obj");
                oo = new ObjectOutputStream(fo);
                oo.writeObject(r);
                oo.close();
                fo.close();
            } catch (IOException ex) {
                Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                try {
                    fo.close();
                } catch (IOException ex) {
                    Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
                }
                try {
                    oo.close();
                } catch (IOException ex) {
                    Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    public boolean metodo()
    {
        while (i<101){
            if(a.nextInt(50000)==3)
            {
                i++;
                 this.jProgressBar1.setValue(i);
            }
        }
        JOptionPane.showMessageDialog(null,"Finalizado");
        return true;
    }
    
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                JFrame.setDefaultLookAndFeelDecorated(true);
                JDialog.setDefaultLookAndFeelDecorated(true);
                new Principal().setVisible(true);
            }
            
          //  JDialog.setDefaultLookAndFeelDecorated(true);
        });
    }

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnEntrar;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JProgressBar jProgressBar1;
    private javax.swing.JTextField txtPass;
    private javax.swing.JTextField txtUser;
    // End of variables declaration//GEN-END:variables
    public void loadObject()
   {
        try {
            FileInputStream fi = null;
            ObjectInputStream oi = null;
            fi = new FileInputStream("c:\\lista.obj");
            oi = new ObjectInputStream(fi);
            txtUser.setText((String)oi.readObject());
        } catch (IOException ex) {
            Logger.getLogger(Entrada.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Entrada.class.getName()).log(Level.SEVERE, null, ex);
        } 
   }
}
